#!/usr/bin/env python
# coding=utf8
'''
Created on 2011-5-1

@author: edison
'''
try:
    import json
except ImportError:
    import simplejson as json
import sys
from daemonizer import daemonize
from Model.DBModel import DBModel
from Model.MongoQ import MongoQ
from taobaoapi.item import TaoBaoItem
import datetime
import time

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

# def getItem (host='127.0.0.1',port='3306',user='mysql',passwd='',database='test'):
#     mysql = DBModel(host=host, port=port, user=user, passwd=passwd, database=database)
#     mysql.opendb()
#     mysql.setTable(tablename = "qin_item")
#     rows = mysql.get_row()
#     dict = {}
# #    regex = re.compile('\d+')
#     
#     for row in rows:
#         item = TaoBaoItem()
#         result = {}
#         jsonstring = item.getItem(row[0])
# #        print jsonstring
#         try:
#             dict = json.loads(jsonstring)
#         except Exception, e:
#             sys.stderr.write('JSON Error %s\n' % e)
#             mysql.close_db()
#             return
# 
#         if not dict.has_key('error_response'): 
#             result['title'] = dict['item_get_response']['item']['title']
#             result['price'] = dict['item_get_response']['item']['price']
# #            result['cat_id'] = dict['item_get_response']['item']['cid']
#             try:
#                 my_cid = mysql.get_cid(tablename="category_mapping", tcid = dict['item_get_response']['item']['cid'])[0][0]
#             except IndexError,e:
#                 my_cid = 12
#                 print "[%s] " % (datetime.datetime.today())
#             result['cat_id'] = my_cid
#             if dict['item_get_response']['item']['stuff_status'] == 'new':
#                 result['stuff_status'] = '全新'
#             elif dict['item_get_response']['item']['stuff_status'] == 'second':
#                 result['stuff_status'] = '二手'
#             else:
#                 result['stuff_status'] = '闲置'
#             result['list_time'] = dict['item_get_response']['item']['list_time']
#             result['delist_time'] = dict['item_get_response']['item']['delist_time']
#             if dict['item_get_response']['item']['location']['city'] == dict['item_get_response']['item']['location']['state']:
#                 result['location'] = dict['item_get_response']['item']['location']['city']
#             else:
#                 result['location'] = '%s %s' % (dict['item_get_response']['item']['location']['state'], dict['item_get_response']['item']['location']['city'])
#             mysql.update_row(tablename = "qin_item", num_iid=row[0], dict = result)
#         else:
#             sys.stderr.write('[%s] Error code:(%s) message %s\n' % (datetime.datetime.today(),dict['error_response']['code'], dict['error_response']['msg']))
#         time.sleep(30.0)
#         
#         
#     mysql.closedb()

def GetConfig(file):
	import ConfigParser
	conf = {}
	cf = ConfigParser.ConfigParser()
	cf.read("etc/job.cfg")
	conf['host'] = cf.get('mysql','host')
	conf['port'] = cf.get('mysql','port')
	conf['user'] = cf.get('mysql', 'user')
	conf['passwd'] = cf.get('mysql','passwd')
 	conf['db'] = cf.get('mysql', 'database')
	
	return conf
	
def decodeData(dict = None):
	result = {}
	if dict.has_key('item_get_response'):
		d = dict['item_get_response']['item']
		result['title'] = d['title']
		result['price'] = d['price']
		result['cat_id'] = d['cid']
		if d['stuff_status'] == 'new':
			result['stuff_status'] = '全新'
		elif d['stuff_status'] == 'second':
			result['stuff_status'] = '二手'
		else:
			result['stuff_status'] = '闲置'
		result['list_time'] = d['list_time']
		result['delist_time'] = d['delist_time']
		if d['location']['city'] == d['location']['state']:
			result['location'] = d['location']['city']
		else:
			result['location'] = '%s %s' % (d['location']['state'], d['location']['city'])
	return result

def itemToMySQL(tb, result):
	cf = GetConfig('etc/job.cfg')
	mysql = DBModel(host=cf['host'], port=cf['port'], user=cf['user'], passwd=cf['passwd'], database=cf['db'])
	mysql.opendb()
	mysql.setTable(tablename = tb)
	try :
		mycid = mysql.get_cid(tablename="category_mapping", tcid = result['cat_id'])[0][0]
		result['cat_id'] = mycid
		# print  mycid
	except IndexError,e:
		result['cat_id'] = 12
		print "[%s] cid Error %s" % (datetime.datetime.today(), e)
	mysql.replace_row(dict = result)
	# for k,v in result.items():
	# 	print "%s => %s" % (k,v)
	mysql.closedb()

def CardToMySQL(tb,result):
	cf = GetConfig('etc/job.cfg')
	mysql = DBModel(host=cf['host'], port=cf['port'], user=cf['user'], passwd=cf['passwd'], database=cf['db'])
	mysql.opendb()
	mysql.setTable(tablename = tb)
	mysql.replace_row(dict = result)
	# for k,v in result.items():
	# 	print "%s => %s" % (k,v)
	mysql.closedb()
	

def HandleData(result, rawdata):
	res = result
	card = {}
	for row in rawdata:
		res['num_iid'] = row['num_iid']
		# res['user'] = row['user']
		res['url'] = row['url']
		res['img_url'] = row['img_url']
		res['width'] = row['width']
		res['height'] = row['height']
		res['created_time'] = row['created_time']
		res['liked'] = 0
		res['repic'] = 0
		itemToMySQL('qin_item', res)

		card['num_iid_id'] = row['num_iid']
		card['user_id'] = row['user_id']
		card['desc'] = row['desc']
		card['pub_time'] = row['created_time']
		card['commented'] = 0
		CardToMySQL('qin_cards', card)
	
def startJob(host = 'localhost'):
	Q = MongoQ(host= host)
	Q.setDB(database='qinzhoukan')
	Q.setCollection(collection='itemqueue')
	
	queue = Q.getDistinct('num_iid')

	if (len(queue) > 0):
		for id in queue:
			taobao = TaoBaoItem()
			jsonString = taobao.getItem(id)
			# print jsonString
			try :
				dict = json.loads(jsonString)
			except Exception, e:
				sys.stderr.write('num_iid is %d JSON Error %s\n' % (id,e))
				
			if not dict.has_key('error_response'):
				result = decodeData(dict)
				rawData = Q.getall({'num_iid':id})
				HandleData(result, rawData)
			else:
				sys.stderr.write('[%s] Error code:(%s) message %s\n' % (datetime.datetime.today(),dict['error_response']['code'], dict['error_response']['msg']))
			Q.remove(dict = {'num_iid':id})
			time.sleep(5.0)
	else :
		print "no Data"
		Q.close()
		return 
	Q.close()

if __name__ == '__main__':
	daemonize(stdout='/tmp/jobrun.log', stderr='/tmp/joberr.log')
	while True:
		startJob(host = '10.0.1.254')

#     import ConfigParser
#     cf = ConfigParser.ConfigParser()
#     cf.read("etc/job.cfg")
#     
#     host = cf.get('mysql','host')
# #    port = cf.get('mysql','port')
#     user = cf.get('mysql', 'user')
#     passwd = cf.get('mysql','passwd')
#     database = cf.get('mysql', 'database')
#     
#     
#     daemonize(stdout='/tmp/jobrun.log', stderr='/tmp/joberr.log')
#     while True:
#         getItem(host=host,user=user,passwd=passwd,database=database)
#         time.sleep(300.0)
